AxoCalculator implements several programming languages. This document describes its implementation of the Fortran language. It is a simplified subset of true Fortran, and includes many useful built in functions and subroutines. The main omission is that statement labels and the "go to" command are not supported. This means that, unlike normal Fortran, lines do not have to start with tab or space characters. All conditional branches and loops must be written using the modern block structured Fortran commands :
if - then - else - endif
do - enddo
do while - enddo
Another non-standard feature is that AxoCalculator outputs the result of any expression which lacks an assignment operator (=). To assign values to two variables, add them and output the result use the following three lines :
a = 10
b = 20
a + b
To execute the above program, first choose "Settings…" under the "Calculator" menu, and make sure the programming language is set to Fortran. Select (highlight) the above three lines using the mouse, then press the "enter" key.
Strings
Character strings are enclosed in single quotes (e.g. 'This is a string'). To include a quote, tab or return in a character string, use
\' for a quote,
\t for a tab
\r for a return.
String variables can be created in two ways. The simplest is to assign a string to a previously unused variable name. For example,
aString = 'Hello world'
As an alternative, use the "NewString" procedure.
NewString (bString)
bString='abcde'
Strings can be appended to one another using the "Concat" function.
NewString (cString)
cString = concat (aString, ' ', bString)
Individual characters in a string can be accessed and manipulated. A string variable behaves like a 255 element array. Each element is one character. Accessing an element returns the ASCII code of that character. The length of the string is stored in the zero indexed element.
Comments are permitted, following a "c" as the first character in a line. An ampersand ( & ) as the first character of a line indicates a continuation line.
For example, select the following 6 lines then press "enter".
c Demonstrate comments, continuation lines and character strings
AxoCalculator programs can interact with the user via standard dialogs. This is done using two built in subroutine calls, "Alert" and "PoseDialog". These subroutines are described in the "Built in Procedures" document. "Alert" is used for simple messages or for returning results. "PoseDialog" is used for requesting one or more numerical values. An example program follows :
PoseDialog ('\r Calculate the volume and surface area of a sphere',
& 'Radius of sphere ',radius)
theSA = 4 * pi * radius ^ 2
theVolume = (4 / 3) * pi * radius ^ 3
Alert ('The volume and surface area of a \r sphere with radius ', radius,
& ' are : \r Volume = ',theVolume,
& '\r Surface area = ',theSA)
Conditional Branch
AxoCalculator supports the standard "if then else endif" statement for conditional branching. A program demonstrating this statement follows :
c This program finds the square root of a number entered by the user
PoseDialog ('\r Find the square root of a number', 'Enter the number', a)
if (a < 0) then
Alert ('Can\'t calculate the square root of a negative number : ',a)
else
b = sqrt (a)
Alert ('\r The square root of ',a,'is ',b)
endif
Loop Commands
AxoCalculator supports two commands for executing a group of statements multiple times. These loop commands are "Do - endDo" and "Do While - endDo". Executable programs demonstrating each of these commands follow.
Note : To interrupt a running program (for example to escape from
an infinite loop) press the "esc" key, or the Cmd-period
key combination.
• The "Do" command
Do i = 1, 5
a = i * 5
b = sin (a)
WriteLn ('sin ( ',a,') = ',b)
endDo
• The "Do While" command
a = 5
Do While (a >= 0)
b = exp (a)
WriteLn ('exp ( ',a,') = ',b)
a = a - 1
endDo
Variable Declaration
Variables created without being declared (as in the above examples) are always "Real" (i.e. floating point). To work with Integer, Boolean or Array variables, they must be declared in standard Fortran fashion. For example :
Integer i
Boolean b
Real r
Real anArr(50)
String aStr
Each variable name is preceded by the type of the variable. For array variables, the size of the array is also specified. Only Real Arrays are supported.
Arrays can be created anywhere in a program using the "NewArray" procedure,
NewArray (anArray, arraySize)
Variables may be declared as either global (available to all programs and subroutines) or local (available only to the currently active procedure). Global variables are preserved until they are explicitly unloaded. Local variables, which are declared within a procedure, are automatically unloaded when the procedure finishes executing. Local variables are declared immediately after a "Subroutine", "Function" or "Program" declaration line (see below), and before the first "begin" statement. Global variables are declared before any "Subroutine", "Function" or "Program" declarations. Examples of both local and global variable declaration can be found in the "Example Programs" document.
Subroutines, Functions and Programs
AxoCalculator's programming language can be extended by loading user defined Subroutines, Functions and Programs. These all have the same basic form :
• a declaration line which includes a name and lists any parameters
• an optional local variable declaration section
• a program section terminating with an "end" statement.
Note : Parameters are always passed by value.
Variable parameters are not supported.
Programs can not have any parameters.
A Subroutine, Function or Program can be loaded by selecting it's text, then pressing "enter" or choosing "Load" under the "Calculator" menu. The only difference is that "Load" will not attempt to execute any lines of code outside the subroutine declaration, and may therefore produce a clearer error message.
To run a Subroutine, Function or Program, type in its name followed by any parameters. A Function may be executed as part of a numerical expression. Subroutines and Functions may call other Subroutines and Functions.
Declaring a Subroutine
Here is an example of how do declare a simple subroutine with a single parameter. This subroutine has no local variable declaration section. To load it, select the following 5 lines, and press "enter". To run it, type "CountTo(10)" then "enter".
Subroutine CountTo (Number)
Do j = 1, Number
Write (j)
endDo
end
Declaring a Function
Here is an example of how do declare a simple Function with a single parameter. A function returns a numerical result, and the type of the result (Real, Integer or Boolean) is specified following the function's parameter list. Load this example function as above.
function Factorial (n)
Real f
Integer j
f = 1
Do j = 1, n
f = f * j
endDo
Factorial = f
end
Here are two examples of how to use the "Factorial" function.
Here is an example of how do declare a simple program. Note that a program has no parameter list, and its name is followed by an optional character string. If present, this string is appended to the calculator menu when the program is loaded. If the second last character of the string is a slash ( / ) then the last character becomes a command key equivalent for running the program. Load the program as above. To run it, type "CountDown" then "enter", or select "Count Down to Lift Off" from the "Calculator" menu.
program CountDown 'Count Down to Lift Off/9'
WriteLn
WriteLn ('Prepare for count down.')
FlushOutput
c • Pause •
Do k = 1, 50
a = exp(2.0)
endDo
c • Start the Count Down •
Do m = 0, 10
j = 10 - m
if (j = 3) WriteLn ('Ignition.')
if (j = 0) then
WriteLn ('Lift Off !!')
else
WriteLn (j)
endif
FlushOutput
Beep
j = j - 1
c • Slow down the count •
Do k = 1, 20
a = exp(2.0)
endDo
endDo
end
Auto-loading a Program
A useful program can be automatically loaded every time AxoCalculator is started up. To auto-load one or more programs, simply place them in the folder "AxoCalculator AutoLoad". This folder must be located in the same folder as the AxoCalculator program.